﻿using System.Collections.Generic;
using System.Web;
using YouHP.BLL;
using YouHP.Model.Group;
using YouHP.Utility;
using System.Text;

namespace YouHP.Pages.Group
{
   public class Topic:GroupBasePage 
    {
        protected int tid;
        protected List<ReplyInfo> ReplyList = new List<ReplyInfo>();
        protected List<TopicInfo> TopicDataListHottest;
        protected List<TopicInfo> TopicDataListLatest;
        protected List<GroupInfo> groupListHotest;
        protected TopicInfo TopicData;
        protected int _pageSize = 10;
        protected int _pageIndex = 1;
        protected int _pageCount = 0;
        protected int _firstpage = 1;
        protected int _prevpage;
        protected int _nextpage;
        protected int _replyCount;
        protected string PagingString;
        protected string FileRealName = "";

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            this.AddLocation("病友小圈子", "/Group/Index.aspx");
            this.AddLocation(GroupData.GroupName,"/Group/Group.aspx?gid="+GroupData.Id);
            this.SetTitle("病友小圈子");
          

            tid = RequestTools.GetQueryInt("tid", -1);
            if (tid != -1)
            {
                if (GroupData.BrowserPermission == Utility.Constants.BrowserLimit)
                {
                    if (GroupUserData.Role == Utility.Constants.RoleAdmin || GroupUserData.Role == Utility.Constants.RoleCreator
                        || GroupUserData.Role == Utility.Constants.RoleMember)
                    {
                         HandleGet();
                    }
                    else
                    {
                        this.AddErrLine("你没有浏览权限，请先加入！");
                    }
                }
                else if (GroupData.BrowserPermission ==Utility.Constants.BrowserOpen)
                {
                    HandleGet();
                }
            }
            else 
            {
                this.AddErrLine("该帖子不存在");
            }

        }


         protected  void HandleGet()
        {

            //this.AddScriptSrc(TinyMCE.Path);
            //this.AddScript(TinyMCE.InitAdvance("rbody"));

            int count = 4;
            TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(count);
            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(count);
            groupListHotest = GroupBLL.Instance.GetGroupListHotest(6);

            _pageIndex = RequestTools.GetQueryInt("page", 1);

           TopicData = TopicBLL.Instance.GetTopicInfo(tid);
         
           string fileName = System.IO.Path.GetFileName(TopicData.TopicFile).ToLower();
           if (!string.IsNullOrEmpty(fileName))
           {
               string[] fileNames = fileName.Split('.');
               string fileEXT = fileNames[fileNames.Length - 1];

               string fileRealNamePrefix = fileNames[fileNames.Length - 2];

               FileRealName = fileRealNamePrefix + "." + fileEXT;
           }
           if (TopicData.Id > 0)
           {
               TopicBLL.Instance.UpdateTopicHits(TopicData.Id);
           }
           else
           {
               this.AddErrLine("主题不存在！");
           }
           this.AddLocation(TopicData.Title, "/Group/Topic.aspx?gid="+GroupData.Id+"&tid="+TopicData.Id);

           ReplyList = ReplyBLL.Instance.GetReplyList(tid, _pageSize, _pageIndex, out _replyCount);

           _pageCount = Utils.GetPageCount(_replyCount, _pageSize);

           string query = HttpContext.Current.Request.Url.Query;
           query = Utils.RemoveQueryItem(query, "page");

           PagingString = this.GetPagingString(_pageIndex, _pageCount,
               "/Group/Topic.aspx" + query, "page");
        }

        protected bool CanReply(TopicInfo info)
        {
            bool retVal = false;
            if (UserData.Id > 0)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanEdit(TopicInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanDelete(TopicInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId || UserData.Id == GroupData.AppUserId || GroupUserData.Role == Constants.RoleAdmin)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanReply(ReplyInfo info)
        {
            bool retVal = false;
            if (UserData.Id > 0)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanEdit(ReplyInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanDelete(ReplyInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId || UserData.Id == GroupData.AppUserId || GroupUserData.Role == Constants.RoleAdmin)
            {
                retVal = true;
            }

            return retVal;
        }
    }
}
